const util = require("../../../utils/util");

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMoreHidden: 1,
    riderList: [],
    riderUid: 0,
    tip: '',
    action: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      oid: options.oid,
      action: options.action
    })
    if(this.data.action == 'addTip'){      
    }else{
      this.getRiderList();
    }    
  },

  // 骑手列表
  getRiderList: function () {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Rider/GetRiderList?createUid=' + app.globalData.userInfo.Uid + '&state=1&page=' + that.data.CurrentPage,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data.Items && res.data.Data.Items.length > 0) {
            let items = res.data.Data.Items;
            if (that.data.CurrentPage != 1) {
              let prelist = that.data.riderList;
              items = prelist.concat(items);
            }
            that.setData({
              riderList: items,
              loadingMoreHidden: res.data.Data.TotalPages
            })
          }
        }
      }
    })
  },
  loadMore: function () {
    var that = this;
    if (that.data.loadingMoreHidden > 1 && that.data.CurrentPage < that.data.loadingMoreHidden) {
      var currPage = that.data.CurrentPage;
      that.setData({
        CurrentPage: currPage + 1
      })
      that.getRiderList();
    } else {
      return
    }
  },
  changeRider: function (e) {
    let uid = e.currentTarget.dataset.uid;
    this.setData({
      riderUid: uid
    })
  },
  cancelAllot: function () {
    this.setData({
      riderUid: 0
    })
  },

  //派单
  sureAllot: function () {
    let that = this;
    let tip = that.data.tip
    if (tip == '') {
      tip = 0
    }
    wx.request({
      url: app.globalData.host + '/api/POrder/AllotOrderByShop?oid=' + that.data.oid + '&riderUid=' + that.data.riderUid + '&tip=' + tip,
      success: function (res) {
        if (res.data.Code == 0) {
          util.showToast('派单成功','success');
          setTimeout(function () {
            wx.navigateBack({delta: 1})
          }, 800)
        } else {
          util.showModal(res.data.Message);        
        }
      }
    })
  },

  //加小费
  addTip: function(){
    let that = this;
    if(that.data.tip<1){
      util.showToast('小费不能低于1元'); return;
    }
    wx.request({
      url: app.globalData.host + '/api/ship/addTip?oid=' + that.data.oid + '&tip=' + that.data.tip,
      success: function (res) {
        if (res.data.Code == 0) {
          util.showToast('小费添加成功','success');
          setTimeout(function () {
            wx.navigateBack({delta: 1})
          }, 800)
        } else {
          util.showModal(res.data.Message);        
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindTip: function (e) {
    if(isNaN(e.detail.value)){
      util.showToast('必须是整数');
      return;
    }
    this.setData({
      tip: parseInt(e.detail.value)
    })
  },
  chooseTip: function (e) {
    let tip = e.currentTarget.dataset.tip;
    this.setData({
      tip: tip
    })
  },

 
})